<template>
	<view class="category-container">
		<scroll-view scroll-y="true" class="left-scroll">
			<view :class="{'left-item':true,active:activeIndex==index}" 
				   v-for="(item,index) in leftCategoryList" 
				   :key="index"
				   @click="activeChange(index)">
				{{item.title}}
			</view>
			
		</scroll-view>
		<scroll-view scroll-y="true" class="right-sroll" @scroll="scrollChange" :scroll-top="scrollTop">
			<view class="right-item" v-for="(item,index) in rightCateogryList" :key="index">
				<!-- 右侧菜标题的显示 -->
				<u-sticky style="top:0rpx">
					<view class="right-item-title">
						{{item.title}}
					</view>
				</u-sticky>
				
				<!-- 商品展示区域 -->
				<view class="right-item-main">
					<view class="goods-item" v-for="(sitem,sindex) in item.goods" :key="sindex" @click="goDetail(sitem)">
						<view class="goods-item-img-box">
							<image :src="`http:${sitem.image}`" mode="" class="goods-img"></image>
						</view>
						<view class="goods-item-title">
							{{sitem.title}}
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>
<script>
	export default{
		data(){
			return{
				leftCategoryList:[],
				activeIndex:0,
				rightCateogryList:[],
				scrollTop:0,
				timer:null
			}
		},
		methods:{
			async getLeftCategory(){
				const result=await uni.$api.homeModules.getLeftCategoryApi()
				this.leftCategoryList=result.data.data
				//首次进入页面调用第一项的内容
				this.getRightCategoryByCid(this.leftCategoryList[0].cid)
				
			},
			activeChange(index){
				this.activeIndex=index;
				let cid=this.leftCategoryList[index].cid
				this.getRightCategoryByCid(cid)
				//将scrollTop变成0
				this.scrollTop=0
			},
			async getRightCategoryByCid(cid){
				console.log('cid',cid);
				const result=await uni.$api.homeModules.getRightCategoryByCidApi(cid)
				console.log('rsult',result.data.data);
				this.rightCateogryList=result.data.data
			},
			scrollChange(e){
				if(this.timer){
					clearTimeout(this.timer)
				}
				this.timer=setTimeout(()=>{
					
					this.scrollTop=e.target.scrollTop
				},500)
			},
			goDetail(goods){
				console.log('goods',goods);
				uni.navigateTo({
					url:`/goodsPck/detail/detail?gid=${goods.gid}`
				})
			}
		},
		onLoad() {
			this.getLeftCategory()
		}
	}
</script>
<style lang="scss">
	.category-container{
		display: flex;
		box-sizing: border-box;
	
		.left-scroll{
			width: 170rpx;
			height: 100vh;
			background-color: #F8F8F8;
			.left-item{
				height: 92rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 24rpx;
				position:relative;
			}
			.active{
				background-color: #fff;
				color: #d4237a;
				&::before{
					display: block;
					content: ' ';
					width: 5rpx;
					height: 60rpx;
					background-color: #d4237a;
					position: absolute;
					top: 50%;
					left:0;
					transform: translateY(-50%);
				}
			}
		}
		.right-sroll{
			flex-grow: 1;
			height: 100vh;
			background-color: #f5f5f5;
			.right-item{
				.right-item-title{
					width: 100%;
					background-color: #fff;
					margin: 10rpx;
					font-size: 26rpx;
					font-weight: bold;
				}
				.right-item-main{
					display: flex;
					flex-wrap: wrap;
					// justify-content: space-around;
					padding: 10rpx;
					margin:10rpx;
					background-color: #fff;
					.goods-item{
						box-sizing: border-box;
						display: flex;
						flex-direction: column;
						border-radius: 10rpx;
						width: 180rpx;
						height: 250rpx;
						align-items: center;
						margin: 5rpx;
						.goods-item-img-box{
							display: flex;
							height: 190rpx;
							justify-content: center;
							.goods-img{
								width: 180rpx;
								height: 180rpx;
							}
						}
						.goods-item-title{
							@include flex_ellipsis(1)
							font-size: 24rpx;
						}
					}
				}
			}
		}
	}
</style>